package com.sc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * 发送消息Controller
 *
 * @author CL
 *
 */
@RestController
public class SendMsgController {

    private static final Logger log = LoggerFactory.getLogger(SendMsgController.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     *
     * @param msg 消息内容
     * @return
     */
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public boolean send(@RequestBody String msg) {
        try {
            for (int i = 0; i <10000 ; i++) {
                rabbitTemplate.convertAndSend("", RabbitMqConfig.QUEUE_NAME, msg);
            }
        } catch (AmqpException e) {
            log.error("发送消息异常：{}", e);
            return false;
        }
        return true;
    }
}